package com.ruoyi.manage.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.manage.domain.JurisdictionScope;
import com.ruoyi.manage.mapper.JurisdictionScopeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.JurisdictionMapper;
import com.ruoyi.manage.domain.Jurisdiction;
import com.ruoyi.manage.service.IJurisdictionService;

/**
 * 辖区Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-04
 */
@Service
public class JurisdictionServiceImpl implements IJurisdictionService 
{
    @Autowired
    private JurisdictionMapper jurisdictionMapper;
    @Autowired
    private JurisdictionScopeMapper jurisdictionScopeMapper;

    /**
     * 查询辖区
     * 
     * @param id 辖区ID
     * @return 辖区
     */
    @Override
    public Jurisdiction selectJurisdictionById(Long id)
    {
        return jurisdictionMapper.selectJurisdictionById(id);
    }

    /**
     * 查询辖区列表
     * 
     * @param jurisdiction 辖区
     * @return 辖区
     */
    @Override
    public List<Jurisdiction> selectJurisdictionList(Jurisdiction jurisdiction)
    {
        return jurisdictionMapper.selectJurisdictionList(jurisdiction);
    }

    /**
     * 新增辖区
     * 
     * @param jurisdiction 辖区
     * @return 结果
     */
    @Override
    public int insertJurisdiction(Jurisdiction jurisdiction)
    {
        jurisdiction.setCreateTime(DateUtils.getNowDate());
        long id = System.currentTimeMillis();
        jurisdiction.setId(id);
        long jurisdictionId = jurisdictionMapper.insertJurisdiction(jurisdiction);
        List<JurisdictionScope> jurisdictionScopeList = jurisdiction.getJurisdictionScopeList();
        for (JurisdictionScope jurisdictionScope : jurisdictionScopeList) {
            jurisdictionScope.setJurisdictionId(id);
            jurisdictionScope.setCreateTime(DateUtils.getNowDate());
            jurisdictionScopeMapper.insertJurisdictionScope(jurisdictionScope);
        }

        return 1;
    }

    /**
     * 修改辖区
     * 先删除辖区范围，然后在新增
     * @param jurisdiction 辖区
     * @return 结果
     */
    @Override
    public int updateJurisdiction(Jurisdiction jurisdiction)
    {
        Long jurisdictionId = jurisdiction.getId();
        jurisdictionScopeMapper.deleteJurisdictionScopeByJurisdictionId(jurisdictionId);
        List<JurisdictionScope> jurisdictionScopeList = jurisdiction.getJurisdictionScopeList();
        for (JurisdictionScope jurisdictionScope : jurisdictionScopeList) {
            jurisdictionScope.setId(null);
            jurisdictionScope.setJurisdictionId(jurisdictionId);
            jurisdictionScope.setCreateTime(DateUtils.getNowDate());
            jurisdictionScopeMapper.insertJurisdictionScope(jurisdictionScope);
        }
        return jurisdictionMapper.updateJurisdiction(jurisdiction);
    }

    /**
     * 批量删除辖区
     * 
     * @param ids 需要删除的辖区ID
     * @return 结果
     */
    @Override
    public int deleteJurisdictionByIds(Long[] ids)
    {
        for (Long id : ids) {
            jurisdictionScopeMapper.deleteJurisdictionScopeByJurisdictionId(id);
        }
        return jurisdictionMapper.deleteJurisdictionByIds(ids);
    }

    /**
     * 删除辖区信息
     * 
     * @param id 辖区ID
     * @return 结果
     */
    @Override
    public int deleteJurisdictionById(Long id)
    {
        jurisdictionScopeMapper.deleteJurisdictionScopeByJurisdictionId(id);
        return jurisdictionMapper.deleteJurisdictionById(id);
    }
}
